//时间格式转换
export const getTime = (time, type) => {
    var date = new Date(time); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();

    switch (type) {
        case 'Y':
            return Y
        case 'M':
            return M
        case 'D':
            return D
        case 'h':
            return h
        case 'm':
            return m
        case 's':
            return s
        case 'YMD':
            return Y + "-" + M + "-" + D;
        case 'YMDhms':
            return Y + "-" + M + "-" + D + ' ' + h + ":" + m + ":" + s;
        //在某些特殊情况下，需要年月日，时间为0
        case 'YMD000':
            return Y + "-" + M + "-" + D + ' ' + '00:00:00';
        case 'YM':
            return Y + "-" + M;
    }
}
//pid转树
export const pidTree = (res) => { //此方法会改变数据源，可以在调用此方法之前深拷贝一下数据，没有深拷贝会出现问题，最好用tree组件的toTree方法
    var _data = {}; // 递归源对象
    var treeData = [];
    for (let i = 0; i < res.length; i++) {
        const element = res[i];
        _data[element.id] = element;
    }
    for (let i = 0; i < res.length; i++) {
        const child = res[i];
        if (_data.hasOwnProperty(child.pid)) {
            const parent = _data[child.pid];
            if (!parent.hasOwnProperty("children")) parent["children"] = [];
            parent["children"].push(child);
        } else {
            treeData.push(child); //把一级放到treeData
        }
    }
    return treeData;
}
//树选择器添加必要属性节点
export const treeSelect = (arr, name, id) => {
    for (let i = 0; i < arr.length; i++) {
        const e = arr[i];
        e.selected = false
        if (e.hasOwnProperty(name)) {
            e['label'] = e[name]
        }
        if (e.hasOwnProperty(id)) {
            e['value'] = e[id]
        }
        if (e.children) {
            treeSelect(e.children, name, id)
        }
    }
    return arr
}
//给树加label节点，第一个参数是要添加label的数组，第二个参数是可以当label的字段名
export const treeTitle = (event, name) => {
    for (let i = 0; i < event.length; i++) {
        const item = event[i];
        if (item.hasOwnProperty(name)) {
            item['label'] = item[name]
        }
        if (item.children) {
            treeTitle(item.children, name)
        }
    }
    return event
}
//添加value，第一个参数是要添加value的数组，第二个参数是可以当value的字段名
export const addValue = (arr, name) => {
    for (let i = 0; i < arr.length; i++) {
        const e = arr[i];
        if (e.hasOwnProperty(name)) {
            e['value'] = e[name]
        }
        if (e.children) {
            addValue(e.children, name)
        }
    }
    return arr
}
//树选择器要加selected属性，在选中时才不会错乱
export const selectedTree = (arr) => {
    for (let i = 0; i < arr.length; i++) {
        const e = arr[i];
        $set(e, 'selected', false);
        if (e.children) {
            selectedTree(e.children)
        }
    }
    return arr
}
//树形转pid
export const moveChildrenLevel = (arr1) => {
    const send = []
    const move = arr2 => {
        arr2.forEach(item => {
            send.push(item)
            if (item.children) {
                const children = Object.assign([], item.children)
                delete item.children
                move(children)
            }
        })
    }
    move(arr1)
    return send
}
//树的回显用（对比id，增加默认勾选属性
export const updateChecked = (tree, id) => {
    for (let i = 0; i < tree.length; i++) {
        const e = tree[i];
        for (let j = 0; j < id.length; j++) {
            const ele = id[j];
            if (e.id == ele) {
                $set(e, 'checked', true);
            }
        }
    }
    return tree
}